package com.softer.wxzj.controller;

import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.aop.annotation.LogRecord;
import com.softer.wxzj.common.aop.enums.LogOperationType;
import com.softer.wxzj.common.aop.enums.LogOperationUnit;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.entity.BaseRegionInfo;
import com.softer.wxzj.entity.SysOperator;
import com.softer.wxzj.entity.SysRole;
import com.softer.wxzj.service.SysOperatorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * UserController class
 *
 * @author zcz
 * @date 2019/4/10
 */
@RestController
@RequestMapping("/sysOper")
@Slf4j
public class SysOperatorController {
    @Autowired
    SysOperatorService sysOperatorService;

    @RequestMapping("/add")
    @LogRecord(description = "新增主管用户", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.INSERT)
    public CommonResult add(SysOperator sysOperator) {
        sysOperator.setCreater(ShiroUserUtil.getUserName());
        return sysOperatorService.insert(sysOperator);
    }

    @RequestMapping("/update")
    @LogRecord(description = "修改主管用户", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.UPDATE)
    public CommonResult update(SysOperator sysOperator) {
        return sysOperatorService.update(sysOperator);
    }

    @RequestMapping("/delete")
    @LogRecord(description = "删除主管用户", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.DELETE)
    public CommonResult delete(String id) {
        return sysOperatorService.delete(id);
    }

    @RequestMapping("/getList")
    @LogRecord(description = "查询系统用户列表", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.SELECT)
    public PageInfo<SysOperator> getUserList(Page page, SysOperator sysOperator) {
        return sysOperatorService.getList(page, sysOperator);
    }

    @RequestMapping("/passwordReset")
    @LogRecord(description = "系统用户密码重置", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.UPDATE)
    public CommonResult passwordReset(@RequestParam("id") String id) {
        return sysOperatorService.resetPassword(id);
    }

    @RequestMapping("/getInfo")
    @LogRecord(description = "查询系统用户详情", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.SELECT)
    public SysOperator getInfo(String id) {
        return sysOperatorService.getInfo(id);
    }

    /**
     * 开启或关闭用户
     *
     * @param id    用户Id
     * @param state 状态（1开启，0关闭）
     * @return 操作结果
     */
    @PostMapping("/operate")
    @LogRecord(description = "开启或关闭用户", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.UPDATE)
    public CommonResult operate(String id, String state) {
        return sysOperatorService.operate(id, state);
    }

    /**
     * 密码修改
     *
     * @param newPassword
     * @param oldPassword
     * @return
     */
    @PostMapping("/updatePassword")
    @LogRecord(description = "密码修改", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.UPDATE)
    public CommonResult updatePassword(@RequestParam("newPassword") String newPassword, @RequestParam("oldPassword") String oldPassword) {
        return sysOperatorService.updatePassword(newPassword, oldPassword);
    }

    /**
     * 页面左上角展示用户
     *
     * @return
     */
    @PostMapping("/getOperAndRole")
    public Map<String, String> getOperAndRole() {
        return sysOperatorService.getOperAndRole();
    }

    @RequestMapping("/fuzzySearch")
    @LogRecord(description = "模糊查询", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.SELECT)
    public PageInfo<SysOperator> fuzzySearch(Page page, String fuzzy) {
        return sysOperatorService.fuzzySearch(page, fuzzy);
    }

    /**
     * 根据企业id查询外网用户列表
     * cdg
     */
    @PostMapping("/getOutUserListByCompanyId")
    @LogRecord(description = "根据企业主账号查询外网用户列表", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.SELECT)
    public PageInfo<SysOperator> getOutUserListByCompanyId(Page page, SysOperator sysOperator) {
        return sysOperatorService.getOutUserListByCompanyId(page, sysOperator);
    }

    /**
     * 页面左上角展示用户
     *
     * @return
     */
    @PostMapping("/selectOperNoByCompanyId")
    public String selectOperNoByCompanyId(String opeNo) {
        return sysOperatorService.selectOperNoByCompanyId(opeNo);
    }

    /**
     * 根据企业类型查询角色List，用于新增外网用户时角色下拉框
     * cdg
     *
     * @return
     */
    @RequestMapping("/getRoleListByCompanyType")
    @LogRecord(description = "根据企业类型查询外网用户角色", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.SELECT)
    public List<SysRole> getRoleListByCompanyType(String operNo) {
        return sysOperatorService.getRoleListByCompanyType(operNo);
    }

    /**
     * 新增外网用户
     * cdg
     */
    @PostMapping("/insertOutUser")
    @LogRecord(description = "新增外网用户", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.INSERT)
    public CommonResult insertOutUser(SysOperator sysOperator) {
        return sysOperatorService.insertOutUser(sysOperator);
    }

    /**
     * 修改外网用户信息
     *
     * @param
     * @return
     */
    @PostMapping("/updateOutOper")
    @LogRecord(description = "修改外网用户信息", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.UPDATE)
    public CommonResult updateOutOper(SysOperator sysOperator) {
        return sysOperatorService.updateOutOper(sysOperator);
    }

    /**
     * 绑定/换绑加密狗
     *
     * @param sysOperator
     * @return
     */
    @PostMapping("/bindUsbKey")
    @LogRecord(description = "外网用户绑定/换绑加密狗", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.UPDATE)
    public CommonResult bindUsbKey(SysOperator sysOperator,String type) {
        return sysOperatorService.bindUsbKey(sysOperator,type);
    }

    /**
     * 根据usbKey获取operList
     *
     * @param usbKey
     * @return
     */
    @PostMapping("/getUsbKeyIsBind")
    @LogRecord(description = "查询加密狗是否被绑定过", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.SELECT)
    public CommonResult getUsbKeyIsBind(String usbKey) {
        return sysOperatorService.getUsbKeyIsBind(usbKey);
    }

    /**
     * 根据账号查询账号信息
     *
     * @param operNo
     * @return
     */
    @PostMapping("/getOperInfoByOperNo")
    @LogRecord(description = "根据账号查询账号信息", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.SELECT)
    public SysOperator getOperInfoByOperNo(String operNo) {
        return sysOperatorService.getOperInfoByOperNo(operNo);
    }

    /**
     * 内网查询管辖区域内机构主账号列表
     *
     * @return
     */
    @PostMapping("/getOrgMainAccount")
    @LogRecord(description = "内网查询管辖区域内机构主账号列表", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.SELECT)
    public PageInfo<SysOperator> getOrgMainAccount(Page page, SysOperator sysOperator) {
        sysOperator.setOperNo(ShiroUserUtil.getUser().getOperNo());
        return sysOperatorService.getOrgMainAccount(page, sysOperator);
    }

    /**
     * 账号启用/停用
     *
     * @return
     */
    @PostMapping("/openOrCloseAccount")
    @LogRecord(description = "账号启用/停用", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.UPDATE)
    public CommonResult openOrCloseAccount(String operNo,String state) {
        return sysOperatorService.openOrCloseAccount(operNo,state);
    }
/**
 * 通过账号和状态查绑定的小区
 */
@PostMapping("/selectOperNoByRegion")
@LogRecord(description = "通过账号和状态查绑定的小区", operationUnit = LogOperationUnit.SYS_OPER, operationType = LogOperationType.UPDATE)
public List<BaseRegionInfo> selectOperNoByRegion(String type,String operNo){
    return sysOperatorService.selectOperNoByRegion(type,operNo);
}

}
